package ch00.ch03;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author lzp
 * @Date 2024-04-06
 */
public class Test {
    public static void main(String[] args) throws Exception {

        //实例化 学生对象
        Student student = new Student();
        student.setName("李四");
        student.setGender("男");
        student.setHobby(new String[]{"吃","喝","玩游戏"});

        //获取类的操作类 对象的class对象
        Class<? extends Student> clazz = student.getClass();

        //判断是否存在注解
        boolean annotationPresent = clazz.isAnnotationPresent(Info.class);
        if (annotationPresent == true){

            Info annotation = clazz.getAnnotation(Info.class);

            String[] hobbys = annotation.hobbys();
            String genders = annotation.genders();

            if (!genders.contains(student.getGender())) {
                throw new RuntimeException("不合法的性别,性别只能是:" + genders);
            }

            Set<String> set1 = new HashSet<>(Arrays.asList(hobbys));
            Set<String> set2 = new HashSet<>(Arrays.asList(student.getHobby()));

            if (!set1.containsAll(set2)){
                throw new RuntimeException("不合法的爱好,爱好只能是:" + Arrays.toString(hobbys));
            }

        }

        System.out.println(student);

    }
}
